using System;
namespace MPEGnet
{
    internal class Transforms
    {
        internal static void Transform_Avg2_AddDct_CAW(int stride, byte[] p1, int p1i, byte[] p2, int p2i, ref DCTBLOCK blk, byte[] outP, int outPi)
        {
            int offs = 0;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r0.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r0.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r0.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r0.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r0.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r0.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r0.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r0.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r1.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r1.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r1.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r1.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r1.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r1.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r1.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r1.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r2.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r2.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r2.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r2.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r2.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r2.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r2.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r2.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r3.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r3.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r3.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r3.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r3.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r3.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r3.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r3.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r4.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r4.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r4.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r4.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r4.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r4.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r4.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r4.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r5.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r5.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r5.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r5.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r5.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r5.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r5.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r5.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r6.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r6.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r6.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r6.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r6.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r6.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r6.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r6.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) + blk.r7.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) + blk.r7.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) + blk.r7.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) + blk.r7.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) + blk.r7.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) + blk.r7.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) + blk.r7.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) + blk.r7.v7 );
                offs += stride;
        }

        internal static void Transform_Avg2_Write(int stride, byte[] p1, int p1i, byte[] p2, int p2i, byte[] outP, int outPi)
        {
            int offs = 0;
            for (int row = 0; row < 8; row++)
            {
            outP[outPi + offs + 0] = (byte)( ((p1[p1i + offs + 0] + p2[p2i + offs + 0] + 1) >> 1) );
            outP[outPi + offs + 1] = (byte)( ((p1[p1i + offs + 1] + p2[p2i + offs + 1] + 1) >> 1) );
            outP[outPi + offs + 2] = (byte)( ((p1[p1i + offs + 2] + p2[p2i + offs + 2] + 1) >> 1) );
            outP[outPi + offs + 3] = (byte)( ((p1[p1i + offs + 3] + p2[p2i + offs + 3] + 1) >> 1) );
            outP[outPi + offs + 4] = (byte)( ((p1[p1i + offs + 4] + p2[p2i + offs + 4] + 1) >> 1) );
            outP[outPi + offs + 5] = (byte)( ((p1[p1i + offs + 5] + p2[p2i + offs + 5] + 1) >> 1) );
            outP[outPi + offs + 6] = (byte)( ((p1[p1i + offs + 6] + p2[p2i + offs + 6] + 1) >> 1) );
            outP[outPi + offs + 7] = (byte)( ((p1[p1i + offs + 7] + p2[p2i + offs + 7] + 1) >> 1) );
                offs += stride;
            }
        }

        internal static void Transform_Avg4_AddDct_CAW(int stride, byte[] pp, int p1i, int p2i, int p3i, int p4i, ref DCTBLOCK blk, byte[] outP, int outPi)
        {
            int offs = 0;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r0.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r0.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r0.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r0.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r0.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r0.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r0.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r0.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r1.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r1.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r1.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r1.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r1.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r1.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r1.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r1.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r2.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r2.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r2.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r2.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r2.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r2.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r2.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r2.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r3.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r3.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r3.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r3.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r3.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r3.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r3.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r3.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r4.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r4.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r4.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r4.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r4.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r4.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r4.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r4.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r5.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r5.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r5.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r5.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r5.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r5.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r5.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r5.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r6.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r6.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r6.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r6.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r6.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r6.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r6.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r6.v7 );
                offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) + blk.r7.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) + blk.r7.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) + blk.r7.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) + blk.r7.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) + blk.r7.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) + blk.r7.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) + blk.r7.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) + blk.r7.v7 );
                offs += stride;
        }

        internal static void Transform_Avg4_Write(int stride, byte[] pp, int p1i, int p2i, int p3i, int p4i, byte[] outP, int outPi)
        {
            int offs = 0;
            for (int row = 0; row < 8; row++)
            {
            outP[outPi + offs + 0] = (byte)( ((pp[p1i + offs + 0] + pp[p2i + offs + 0] + pp[p3i + offs + 0] + pp[p4i + offs + 0] + 2) >> 2) );
            outP[outPi + offs + 1] = (byte)( ((pp[p1i + offs + 1] + pp[p2i + offs + 1] + pp[p3i + offs + 1] + pp[p4i + offs + 1] + 2) >> 2) );
            outP[outPi + offs + 2] = (byte)( ((pp[p1i + offs + 2] + pp[p2i + offs + 2] + pp[p3i + offs + 2] + pp[p4i + offs + 2] + 2) >> 2) );
            outP[outPi + offs + 3] = (byte)( ((pp[p1i + offs + 3] + pp[p2i + offs + 3] + pp[p3i + offs + 3] + pp[p4i + offs + 3] + 2) >> 2) );
            outP[outPi + offs + 4] = (byte)( ((pp[p1i + offs + 4] + pp[p2i + offs + 4] + pp[p3i + offs + 4] + pp[p4i + offs + 4] + 2) >> 2) );
            outP[outPi + offs + 5] = (byte)( ((pp[p1i + offs + 5] + pp[p2i + offs + 5] + pp[p3i + offs + 5] + pp[p4i + offs + 5] + 2) >> 2) );
            outP[outPi + offs + 6] = (byte)( ((pp[p1i + offs + 6] + pp[p2i + offs + 6] + pp[p3i + offs + 6] + pp[p4i + offs + 6] + 2) >> 2) );
            outP[outPi + offs + 7] = (byte)( ((pp[p1i + offs + 7] + pp[p2i + offs + 7] + pp[p3i + offs + 7] + pp[p4i + offs + 7] + 2) >> 2) );
                offs += stride;
            }
        }

        internal static void Transform_AddDct_CAW(int stride, byte[] inPlane, int p1i, ref DCTBLOCK blk, byte[] outP, int outPi)
        {
            int offs = 0;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r0.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r0.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r0.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r0.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r0.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r0.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r0.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r0.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r1.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r1.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r1.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r1.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r1.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r1.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r1.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r1.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r2.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r2.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r2.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r2.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r2.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r2.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r2.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r2.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r3.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r3.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r3.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r3.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r3.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r3.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r3.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r3.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r4.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r4.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r4.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r4.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r4.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r4.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r4.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r4.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r5.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r5.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r5.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r5.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r5.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r5.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r5.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r5.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r6.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r6.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r6.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r6.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r6.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r6.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r6.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r6.v7 );
            offs += stride;
            outP[outPi + offs + 0] = MPEGnet.video.BClamp( inPlane[p1i + offs + 0] + blk.r7.v0 );
            outP[outPi + offs + 1] = MPEGnet.video.BClamp( inPlane[p1i + offs + 1] + blk.r7.v1 );
            outP[outPi + offs + 2] = MPEGnet.video.BClamp( inPlane[p1i + offs + 2] + blk.r7.v2 );
            outP[outPi + offs + 3] = MPEGnet.video.BClamp( inPlane[p1i + offs + 3] + blk.r7.v3 );
            outP[outPi + offs + 4] = MPEGnet.video.BClamp( inPlane[p1i + offs + 4] + blk.r7.v4 );
            outP[outPi + offs + 5] = MPEGnet.video.BClamp( inPlane[p1i + offs + 5] + blk.r7.v5 );
            outP[outPi + offs + 6] = MPEGnet.video.BClamp( inPlane[p1i + offs + 6] + blk.r7.v6 );
            outP[outPi + offs + 7] = MPEGnet.video.BClamp( inPlane[p1i + offs + 7] + blk.r7.v7 );
            offs += stride;
        }
        internal static void UnZigzag(short[] shorts, out DCTBLOCK block)
        {
            block.r0.v0 = shorts[0];
            block.r0.v1 = shorts[1];
            block.r1.v0 = shorts[2];
            block.r2.v0 = shorts[3];
            block.r1.v1 = shorts[4];
            block.r0.v2 = shorts[5];
            block.r0.v3 = shorts[6];
            block.r1.v2 = shorts[7];
            block.r2.v1 = shorts[8];
            block.r3.v0 = shorts[9];
            block.r4.v0 = shorts[10];
            block.r3.v1 = shorts[11];
            block.r2.v2 = shorts[12];
            block.r1.v3 = shorts[13];
            block.r0.v4 = shorts[14];
            block.r0.v5 = shorts[15];
            block.r1.v4 = shorts[16];
            block.r2.v3 = shorts[17];
            block.r3.v2 = shorts[18];
            block.r4.v1 = shorts[19];
            block.r5.v0 = shorts[20];
            block.r6.v0 = shorts[21];
            block.r5.v1 = shorts[22];
            block.r4.v2 = shorts[23];
            block.r3.v3 = shorts[24];
            block.r2.v4 = shorts[25];
            block.r1.v5 = shorts[26];
            block.r0.v6 = shorts[27];
            block.r0.v7 = shorts[28];
            block.r1.v6 = shorts[29];
            block.r2.v5 = shorts[30];
            block.r3.v4 = shorts[31];
            block.r4.v3 = shorts[32];
            block.r5.v2 = shorts[33];
            block.r6.v1 = shorts[34];
            block.r7.v0 = shorts[35];
            block.r7.v1 = shorts[36];
            block.r6.v2 = shorts[37];
            block.r5.v3 = shorts[38];
            block.r4.v4 = shorts[39];
            block.r3.v5 = shorts[40];
            block.r2.v6 = shorts[41];
            block.r1.v7 = shorts[42];
            block.r2.v7 = shorts[43];
            block.r3.v6 = shorts[44];
            block.r4.v5 = shorts[45];
            block.r5.v4 = shorts[46];
            block.r6.v3 = shorts[47];
            block.r7.v2 = shorts[48];
            block.r7.v3 = shorts[49];
            block.r6.v4 = shorts[50];
            block.r5.v5 = shorts[51];
            block.r4.v6 = shorts[52];
            block.r3.v7 = shorts[53];
            block.r4.v7 = shorts[54];
            block.r5.v6 = shorts[55];
            block.r6.v5 = shorts[56];
            block.r7.v4 = shorts[57];
            block.r7.v5 = shorts[58];
            block.r6.v6 = shorts[59];
            block.r5.v7 = shorts[60];
            block.r6.v7 = shorts[61];
            block.r7.v6 = shorts[62];
            block.r7.v7 = shorts[63];
        }
    }
}

